package frist50;

import or.T0445.ListNode;

public class T0023 {
    public ListNode mergeKLists(ListNode[] lists) {
        ListNode head = new ListNode();
        ListNode tmp = head;
        if (lists.length == 0) return null;
        while (true) {
            int min = Integer.MAX_VALUE;
            int id = -1;
            for (int i = 0; i < lists.length; i++) {
                if (lists[i] != null && min > lists[i].val) {
                    min = lists[i].val;
                    id = i;
                }
            }
            if (id == -1) break;
            tmp.next=new ListNode(min);
            tmp=tmp.next;
            lists[id]=lists[id].next;
        }
        return head.next;
    }
}
